package com.wonderGirls.swGirls2.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.wonderGirls.swGirls2.MainActivity;
import com.wonderGirls.swGirls2.offer.OfferHelper;

public class ResourceManager {
	public static final String BaseDir = "images";
	public static final String NamePath = "name.txt";
	public static final String PageDir = "pages";
	// 每张图片2金币
	public static final int PricePerImage = 2;
	public static final int UnlockAllPrice = 100;

	public static List<Integer> indexList = new ArrayList<Integer>();
	public static int currentGold = 50;
	public static int PageCount = 0;
	public static HashMap<Integer, String> indexToGirlNameMap = new HashMap<Integer, String>();
	public static HashMap<Integer, String> indexToResDirMap = new HashMap<Integer, String>();
	public static HashMap<Integer, Integer> indexToCountMap = new HashMap<Integer, Integer>();
	public static HashMap<Integer, Boolean> indexToIsLockedMap = new HashMap<Integer, Boolean>();
	static SharedPreferences settings;
	static MainActivity context;
	static String status = "";

	static String GOLD_KEY = "golds";

	public static void init(MainActivity context) {
		ResourceManager.context = context;
		settings = context.getPreferences(Activity.MODE_PRIVATE);

		indexList.clear();
		indexToGirlNameMap.clear();
		indexToResDirMap.clear();
		indexToCountMap.clear();
		indexToIsLockedMap.clear();

		// 从assets下获取所有的资源并构建集合
		String[] list = null;
		try {
			list = context.getResources().getAssets().list(BaseDir);

			int i = 0;
			for (String name : list) {
				String dir = BaseDir + File.separator + name;
				String[] images = context.getResources().getAssets().list(dir);
				if (images.length > 0) {
					int count = images.length - 2;
					indexList.add(i);
					indexToGirlNameMap
							.put(i, getName(dir) + "(" + count + "P)");
					indexToResDirMap.put(i, dir);
					indexToCountMap.put(i, count);
					indexToIsLockedMap.put(i, getLockStatus(i));
					i++;
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
		}

		try {
			list = context.getResources().getAssets().list(PageDir);
			PageCount = list.length;
		} catch (IOException e) {
			e.printStackTrace();
		}

		initGold();
	}

	private static String getName(String dir) {
		String filePath = dir + File.separator + NamePath;
		InputStreamReader inputStreamReader = null;
		try {
			inputStreamReader = new InputStreamReader(context.getAssets().open(
					filePath), "utf-8");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		BufferedReader reader = new BufferedReader(inputStreamReader);
		String line = "";
		try {
			line = reader.readLine();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return line;
	}

	private static void initGold() {
		currentGold = settings.getInt(GOLD_KEY, 30);
	}

	public static void saveGold() {
		Editor editor = settings.edit();
		editor.putInt(GOLD_KEY, currentGold);
		editor.commit();
	}

	public static int getPrice(int index) {
		return indexToCountMap.get(index) * PricePerImage;
	}

	public static Bitmap getBitmapFromAsset(String strName) throws IOException {
		AssetManager assetManager = context.getAssets();
		InputStream istr = assetManager.open(strName);
		byte[] img = new byte[istr.available()];
		istr.read(img, 0, img.length);
		istr.close();
		Bitmap bitmap = BitmapFactory.decodeByteArray(img, 1, img.length - 1);
		return bitmap;
	}

	private static boolean getLockStatus(int index) {
		// //TODO 为了截图
		// return false;
		// 前三套免费
		if (index <= 2)
			return false;
		else
			return settings.getBoolean("l" + index, true);
	}

	public static synchronized void unlockAll() {
		if (currentGold >= UnlockAllPrice) {
			currentGold -= UnlockAllPrice;
			saveGold();

			Editor editor = settings.edit();
			for (int key : indexToIsLockedMap.keySet()) {
				indexToIsLockedMap.put(key, false);
				editor.putBoolean("l" + key, false);
			}
			editor.commit();
			// 更新界面上的金币
			MainActivity.instance.updateGold();
		} else {
			// 提示金币不足
			OfferHelper.showOffer(MainActivity.instance, null, "金币不足是否免费赚取金币");
		}
	}

	public static synchronized void unlockGallery(int index) {
		if (indexToIsLockedMap.get(index)) {
			int price = getPrice(index);
			if (currentGold >= price) {
				currentGold -= price;
				saveGold();
				indexToIsLockedMap.put(index, false);
				Editor editor = settings.edit();
				editor.putBoolean("l" + index, false);
				editor.commit();

				// 更新界面上的金币
				MainActivity.instance.updateGold();
			} else {
				// 提示金币不足
				OfferHelper.showOffer(MainActivity.instance, null,
						"金币不足是否免费赚取金币");
			}
		}
	}
}
